namespace Brjnk.GpxExplorer.UI.ValidationRules
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Data;
    using Brjnk.Common;

    /// <summary>
    /// </summary>
    public class PaperSizePickerVr : ValidationRule
    {
        public PaperSizePickerVr()
        {
            this.ValidationStep = System.Windows.Controls.ValidationStep.CommittedValue;
        }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            PaperSize predefinedPaper;
            //bool landscape;
            Distance customPaperWidth;
            Distance customPaperHeight;
            bool useCustomSize;

            object o;
            
            var bg = value as BindingGroup;
            predefinedPaper =(PaperSize) bg.GetValue(bg.Items[0], "PredefinedPaper");
            //landscape = (bool)bg.GetValue(bg.Items[0], "Landscape");
            customPaperWidth =  (Distance)(bg.GetValue(bg.Items[0], "CustomPaperWidth"));
            customPaperHeight = (Distance)(bg.GetValue(bg.Items[0], "CustomPaperHeight"));
            useCustomSize = (bool)bg.GetValue(bg.Items[0], "UseCustomSize");

            if (useCustomSize)
            {
                if (customPaperHeight < 0 || customPaperHeight > 5) { return new ValidationResult(false, "Invalid paper height"); }
                if (customPaperWidth < 0 || customPaperWidth > 5) { return new ValidationResult(false, "Invalid paper width"); }
            }
            else
            {
                if (predefinedPaper == null) { return new ValidationResult(false, "Unselected paper size"); }
            }

            return ValidationResult.ValidResult;
        }
    }
}
